﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Runtime.InteropServices;// For COMException 
using System.Collections;
using System.Net;
namespace IPRAN_PORT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string filename=null;
            openFileDialog1.InitialDirectory = @"d:\";
            openFileDialog1.Filter="EXCEl文件(*.XLSX)|*.XLSX|所有文件(*.*)|*.*" ;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filename = openFileDialog1.FileName;
            }
            if (filename==null)
            {
                return;
            }
            Excel.Application excel1 = new Excel.Application();
            Excel.Workbook wk= excel1.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value,Missing.Value,Missing.Value);
            Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wk.Worksheets[1];
           // MessageBox.Show(ws.UsedRange.Rows.Count.ToString()+"  "+ws.UsedRange.Columns.Count.ToString());
            int height;
            for (height = 1; height < 100; ++height)
                if (((Excel.Range)ws.Cells[height, 1]).Text.ToString()=="")
                    break;
            List<port> array = new List<port>();
            for (int i = 2; i < height; ++i)
            {
                port temp = new port(); ;
                temp.sne=((Excel.Range)ws.Cells[i, 2]).Text.ToString();
                temp.portmode = ((Excel.Range)ws.Cells[i, 3]).Text.ToString();
                temp.sport = ((Excel.Range)ws.Cells[i, 4]).Text.ToString();
                temp.sip = ((Excel.Range)ws.Cells[i, 5]).Text.ToString();
                temp.smac = ((Excel.Range)ws.Cells[i, 6]).Text.ToString();
                temp.dne= ((Excel.Range)ws.Cells[i, 7]).Text.ToString();
                temp.dport = ((Excel.Range)ws.Cells[i, 8]).Text.ToString();
                temp.dip= ((Excel.Range)ws.Cells[i, 9]).Text.ToString();
                temp.dmac = ((Excel.Range)ws.Cells[i, 10]).Text.ToString();
                array.Add(temp);
            }
            for (int i = 0; i < array.Count; ++i)
            {
                if (array[i].portmode == "TP")
                {
                    int tempip = port.IPToNumber(array[i].sip);
                    tempip = tempip & 3;
                    if (tempip != 1)
                    {
                        array[i].isErr = true;
                        array[i].ErrCode = "IP地址错误";
                        continue;
                    }
                    int dstip = port.IPToNumber(array[i].sip) + 1;
                    array[i].dip = port.NumberToIP(dstip);
                }
                else if (array[i].portmode == "L3")
                {
                    int tempip = port.IPToNumber(array[i].sip);
                    tempip = tempip & 3;
                    if (tempip != 1&&tempip!=2)
                    {
                        array[i].isErr = true;
                        array[i].ErrCode = "IP地址错误";
                        continue;
                    }
                    if (array[i].sne == array[i].dne)
                    {
                        array[i].isErr = true;
                        array[i].ErrCode = "源宿网元错误";
                        continue;
                    }
                    for (int j = 0; j < array.Count; ++j)
                    {
                        if (array[i].sne == array[j].dne && array[i].sport == array[j].dport&&array[i].dne==array[j].sne&&array[i].dport==array[j].sport)
                        {
                            int tempip2 = port.IPToNumber(array[j].sip);
                            tempip2 = tempip2 & 3;
                            if (tempip2 != 1 && tempip2 != 2)
                            {
                                array[i].isErr = true;
                                array[i].ErrCode = "对端IP地址错误";
                                continue;
                            }
                            if (Math.Abs(tempip2 - tempip) != 1)
                            {
                                array[i] .isErr= false;
                                array[i].ErrCode = "IP地址不在同一个网段";
                                continue;
                            }
                            array[i].dip = array[j].sip;
                            array[i].dmac = array[j].smac;
                            break;
                        }
                        else if (j == array.Count - 1)
                        {
                            array[i].isErr = true;
                            array[i].ErrCode = "无法查找到对端网元";
                        }
                    }  
                }
            }
            int tmpmac = 1001;
            for (int i = 0; i < array.Count; ++i)//将修改后的内容写入excel
            {
                
                if (array[i].isErr)
                {
                    Excel.Range range = ws.get_Range(ws.Cells[i + 2, 1], ws.Cells[i + 2, 10]);
                    range.Interior.ColorIndex = 6;
                    ((Excel.Range)ws.Cells[i + 2, 11]).Value2 = array[i].ErrCode;
                }
                else
                {
                    
                    ((Excel.Range)ws.Cells[i+2, 9]).Value2= array[i].dip;
                    if (array[i].portmode == "L3")
                    {
                        ((Excel.Range)ws.Cells[i + 2, 10]).Value2 = array[i].dmac;

                    }
                    else if (array[i].portmode == "TP")
                    {
                        ((Excel.Range)ws.Cells[i + 2, 10]).Value2 = "ACE08700" + tmpmac.ToString();
                        array[i].dmac = "ACE08700" + tmpmac.ToString();
                        tmpmac++;
                    }

                       
                }
            }
            Excel.Worksheet ws2 = (Microsoft.Office.Interop.Excel.Worksheet)wk.Worksheets[2];
            ws2.Name = "ARP";
            ws2.Cells.Clear();
            ((Excel.Range)ws2.Cells[1, 1]).Value2 = "本端网元";
            ((Excel.Range)ws2.Cells[1, 2]).Value2 = "对端网元";
            ((Excel.Range)ws2.Cells[1, 3]).Value2 = "对端IP";
            ((Excel.Range)ws2.Cells[1, 4]).Value2 = "对端MAC";
            for (int i = 0; i < array.Count; i++)//生成arp
            {
                if (array[i].isErr)
                    continue;
                ((Excel.Range)ws2.Cells[i + 2, 1]).Value2 = array[i].sne;
                ((Excel.Range)ws2.Cells[i + 2, 2]).Value2 = array[i].dne;
                ((Excel.Range)ws2.Cells[i + 2, 3]).Value2 = array[i].dip;
                ((Excel.Range)ws2.Cells[i + 2, 4]).Value2 = array[i].dmac;

            }






                //MessageBox.Show(array[0].sne);
            wk.Save();
            excel1.Quit();
            excel1 = null;
            GC.Collect();

            MessageBox.Show("完成","处理完成");
        }
    }
}
